package com.neusoft.data.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neusoft.data.mapper.SerKeyQuestionMapper;
import com.neusoft.data.domain.SerKeyQuestion;
import com.neusoft.data.service.ISerKeyQuestionService;

/**
 * 关键问题Service业务层处理
 * 
 * @author slh
 * @date 2023-12-29
 */
@Service
public class SerKeyQuestionServiceImpl implements ISerKeyQuestionService 
{
    @Autowired
    private SerKeyQuestionMapper serKeyQuestionMapper;

    /**
     * 查询关键问题
     * 
     * @param questionId 关键问题主键
     * @return 关键问题
     */
    @Override
    public SerKeyQuestion selectSerKeyQuestionByQuestionId(Long questionId)
    {
        return serKeyQuestionMapper.selectSerKeyQuestionByQuestionId(questionId);
    }

    /**
     * 查询关键问题列表
     * 
     * @param serKeyQuestion 关键问题
     * @return 关键问题
     */
    @Override
    public List<SerKeyQuestion> selectSerKeyQuestionList(SerKeyQuestion serKeyQuestion)
    {
        return serKeyQuestionMapper.selectSerKeyQuestionList(serKeyQuestion);
    }

    /**
     * 新增关键问题
     * 
     * @param serKeyQuestion 关键问题
     * @return 结果
     */
    @Override
    public int insertSerKeyQuestion(SerKeyQuestion serKeyQuestion)
    {
        serKeyQuestion.setCreateTime(DateUtils.getNowDate());
        return serKeyQuestionMapper.insertSerKeyQuestion(serKeyQuestion);
    }

    /**
     * 修改关键问题
     * 
     * @param serKeyQuestion 关键问题
     * @return 结果
     */
    @Override
    public int updateSerKeyQuestion(SerKeyQuestion serKeyQuestion)
    {
        serKeyQuestion.setUpdateTime(DateUtils.getNowDate());
        return serKeyQuestionMapper.updateSerKeyQuestion(serKeyQuestion);
    }

    /**
     * 批量删除关键问题
     * 
     * @param questionIds 需要删除的关键问题主键
     * @return 结果
     */
    @Override
    public int deleteSerKeyQuestionByQuestionIds(Long[] questionIds)
    {
        return serKeyQuestionMapper.deleteSerKeyQuestionByQuestionIds(questionIds);
    }

    /**
     * 删除关键问题信息
     * 
     * @param questionId 关键问题主键
     * @return 结果
     */
    @Override
    public int deleteSerKeyQuestionByQuestionId(Long questionId)
    {
        return serKeyQuestionMapper.deleteSerKeyQuestionByQuestionId(questionId);
    }

    @Override
    public String importUser(List<SerKeyQuestion> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入用户不存在");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (SerKeyQuestion serKeyQuestion : list) {
            try {
                SerKeyQuestion q = serKeyQuestionMapper.selectSerKeyQuestionByQuestionId(serKeyQuestion.getQuestionId());

                if (StringUtils.isNull(q)) {
                    serKeyQuestionMapper.insertSerKeyQuestion(serKeyQuestion);
                    successNum ++;
                    successMsg.append("<br/>" + successNum + "、会话 " + serKeyQuestion.getQuestionName() + " 导入成功：");
                } else if (updateSupport) {
                    serKeyQuestionMapper.updateSerKeyQuestion(serKeyQuestion);
                    successMsg.append("<br/>" + successNum + "、会话 " + serKeyQuestion.getQuestionName() + " 更新成功：");
                } else {
                    failureNum ++;
                    failureMsg.append("<br/>" + failureNum + "、会话 " + serKeyQuestion.getQuestionName() + " 已存在：");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、问题 " + serKeyQuestion.getQuestionName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据格式不正确，错误如下");
            throw  new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "导入成功共 " + successNum + " 条数据");
        }

        return successMsg.toString();
    }
}
